app.controller('StartListCtrl', function ($scope, ajax, $modal, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.reload = $scope.page.loader('starting/bk/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);
	$scope.queryMap = {};
	$scope.query = function () { $scope.reload(1, $scope.queryMap); };

	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		var modalInstance = $modal.open({
			templateUrl: 'template/booking/start/writeModal.html?v=e9e5223ebc',
			controller: 'StartModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			if (param.type == 'update') {
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'starting/bk/update';
				$scope.page.mod(updater, $scope.reload);
			} else if (param.type == 'insert') {
				inserter.fields = param.data;
				inserter.resource = 'starting/bk/insert';
				$scope.page.add(inserter, $scope.reload);
			}
		});
	};

	$scope.ids = [];
	$scope.checks = false;//默认未选中
	$scope.ckAll = false;//默认未选中
	$scope.choseArr = [];

	/*单选*/
	$scope.check = function (val) {
		if ($scope.ids.indexOf(val) < 0) {
			$scope.ids.push(val);
		} else {
			$scope.ids.splice($scope.ids.indexOf(val), 1);
		}
	};

	/*全选*/
	$scope.checkAll = function (c, v) {
		if (c == true) {
			$scope.checks = true;
			$scope.choseArr = v;
			//添加所选Id
			for (var i = 0; i < $scope.choseArr.length; i++) {
				$scope.ids.push($scope.choseArr[i].id);
			}
		} else {
			$scope.checks = false;
			$scope.ids = [];
		}
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		$scope.delModal(size, current_data, submit_type).result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'starting/bk/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload);
			} else if (param.type == 'deleteAll') {
				if ($scope.ids == "" || $scope.ids == null) {
					toaster.pop('warning', '提示', '请至少选择一条要删除的信息！');
					return;
				}
				deleter.datalist = $scope.ids;
				deleter.resource = 'starting/bk/batch/delete';
				$scope.page.del(deleter, $scope.reload, $scope.queryMap).then(function () { $scope.ids = null; });
			}
		});
	};

	//设置热门站点
	$scope.populars = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		$modal.open({
			templateUrl: 'template/booking/start/popular.html?v=2052903461',
			controller: 'PopularModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		});
	};

	//二维码生成
	$scope.qr = function (start) {
		$modal.open({
			templateUrl: 'template/booking/start/qrcode.html?v=5cbfb64beb',
			controller: 'QRCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": start
					};
				}
			}
		});
	};
});
